<template>
  <div class="auth-page">
    <div class="container page">
      <div class="row">
        <div class="col-md-6 offset-md-3 col-xs-12">
          <h1 class="text-xs-center">{{ isLogin ? 'Sign in' : 'Sign up' }}</h1>
          <p class="text-xs-center">
            <nuxt-link v-if="isLogin" to="/register">
              Need an account?
            </nuxt-link>
            <nuxt-link v-else to="/login">Have an account?</nuxt-link>
          </p>

          <ul v-if="errMsg !== null" class="error-messages">
            <template v-for="(e, index) in errMsg">
              <!-- 由于拉勾源和conduit源的数据不同 -->
              <!-- 所以写了两份 -->
              <template v-if="typeof e === 'object'">
                <template v-for="i in e">
                  <li :key="index">{{ index }} {{ i }}</li>
                </template>
              </template>
              <!-- 拉勾源：无数组 -->
              <template v-else>
                <li :key="index">{{ index }} {{ e }}</li>
              </template>
            </template>
          </ul>

          <form @submit.prevent="handleSubmit">
            <fieldset v-if="!isLogin" class="form-group">
              <input
                v-model="user.username"
                class="form-control form-control-lg"
                type="text"
                placeholder="Your Name"
                required
              />
            </fieldset>
            <fieldset class="form-group">
              <input
                v-model="user.email"
                class="form-control form-control-lg"
                type="email"
                placeholder="Email"
                required
              />
            </fieldset>
            <fieldset class="form-group">
              <input
                v-model="user.password"
                class="form-control form-control-lg"
                type="password"
                placeholder="Password"
                required
                minlength="8"
              />
            </fieldset>
            <button class="btn btn-lg btn-primary pull-xs-right">
              {{ isLogin ? 'Sign in' : 'Sign up' }}
            </button>
          </form>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { login, register } from '@/api/user'

export default {
  middleware: 'notAuth',
  name: 'LoginIndex',
  data() {
    return {
      errMsg: null,
      user: {
        username: '',
        email: '',
        password: '',
      },
    }
  },
  computed: {
    isLogin() {
      return this.$route.name === 'login'
    },
  },
  methods: {
    handleMethods(data) {
      if (this.isLogin) {
        delete data.username
        return login(data)
      } else return register(data)
    },
    handleSubmit() {
      const user = { ...this.user }
      this.handleMethods(user)
        .then(res => {
          this.$store.dispatch('login', res.user)
          this.$router.push('/')
        })
        .catch(e => {
          this.errMsg = e.response.data.errors
        })
    },
  },
}
</script>

<style></style>
